﻿package app.view.components.sections.expo 
{
	import app.model.RemoteProxy;
	import app.model.types.SectionType;
	import app.model.vo.SectionVO;
	import app.view.components.buttons.GlamurBtnBase;
	import app.view.components.guiDima.slideShowD.Image;
	import app.view.components.header.HeadMarkerBase;
	import app.view.components.sections.LabeledSectionBase;
	import app.view.events.UIEvent;
	import flash.display.Loader;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.system.ApplicationDomain;
	import flash.system.LoaderContext;
	import flash.text.TextField;
	import gs.TweenLite;
	
	/**
	 * 1.1.5.1.1 СОБЫТИЕ НА СЕГОДНЯ
	 * @author Dmitryi_Koruagin, icq:585466579, dmitryikoruagin@mail.ru, skype: ok_ru1
	 */
	public class ExpoTodayProgrammExtendsDBase extends LabeledSectionBase
	{
		// app.view.components.sections.expo.ExpoTodayProgrammExtendsDBase
		
		public var textEvent:TextField;
		public var dates:TextField;
		public var time:TextField;
		public var bgdImage:Sprite;
		
		public var calendar_btn:GlamurBtnBase;
		public var programm_btn:GlamurBtnBase;
		
		public var narrow_left:NarrowD;
		public var narrow_right:NarrowD;
		
		private var _currentId:int;
		private var _isLast:Boolean;
		private var _isNext:Boolean;
		
		private static const TIME_SHOW_NAVIGATE:Number = 0.3;
		private static const TIME_HIDE_NAVIGATE:Number = 0.2;
		private var _arrayDataItem:Array;
		private var _image:Image;
		
		public function ExpoTodayProgrammExtendsDBase() 
		{
			super();
			
		}
		
		override protected function init():void
		{
			super.init();
			
			_isLast = false;
			_isNext = false;
			narrow_left.alpha = 0;
			narrow_right.alpha = 0;
			narrow_left.selected = true;
			narrow_right.selected = true;
			
			textEvent.wordWrap = true;
			textEvent.multiline= true;
			textEvent.text = '';
			
			_image = new Image(bgdImage.width, bgdImage.height, Image.RESIZE_MAX, Image.ALIGN_CENTER);
			addChild(_image);
			_image.x = bgdImage.x;
			_image.y = bgdImage.y;
			
			
			programm_btn.visible = false;
		}
		
		private function setEvents():void
		{
			calendar_btn.addEventListener(MouseEvent.CLICK, onCLickCalendarBtn);
			programm_btn.addEventListener(MouseEvent.CLICK, onCLickProgrammBtn);
			
		}
		
		
		private function removeEvents():void
		{
			calendar_btn.removeEventListener(MouseEvent.CLICK, onCLickCalendarBtn);
			programm_btn.removeEventListener(MouseEvent.CLICK, onCLickProgrammBtn);
			
			narrow_left.removeEventListener(MouseEvent.CLICK, onClickLeftBtn);
			narrow_right.removeEventListener(MouseEvent.CLICK, onClickRightBtn);
			
		}
		
		
		override public function onDestroy():void 
		{
			// чистим что тут есть и вызываем супера
			removeEvents();
			
			_image.destroy();
			removeChild(_image);
			
			calendar_btn.destroy();
			programm_btn.destroy();
			
			super.onDestroy();
		};
		
		override public function get data():Object { return _data; };
		
		override public function set data(value:Object):void 
		{
			super.data = value;
			textEvent.wordWrap = true;
			textEvent.multiline = true;
			
			
			//trace('data!!!+++', _data.params.data.params.data);
			//
			//trace('data!!!+++',_data.params.selectedId);
			
			
			/*for (var name:String in _data.params.data.params.data) 
			{
				trace('name+ = ', name);
				
			}*/
			
			_arrayDataItem = new Array();
			
			//trace('555',_data.params.data.data.event);
			//trace('777',_data.params.data.fullDateString);
			//
			//trace('888',_data.params.today.data.event);
			//trace('999',_data.params.today.fullDateString);
			
			if (_data.params.today.data.event==undefined) 
			{
				programm_btn.visible = false;
			}else
			{
				if (_data.params.data.fullDateString==_data.params.today.fullDateString) 
				{
					programm_btn.visible = false;
				}else
				{
					programm_btn.visible = true;
				}
				
			}
			
			//programm_btn.visible = true;
			
			for each(var object:* in _data.params.data.data.event)
			{
				//trace('***');
				_arrayDataItem.push(object);
			}
			
			//trace('len = ', _arrayDataItem[_currentId].text[1]);
			//trace('len = ', _arrayDataItem[_currentId].text[2]);
			//trace('len = ', _arrayDataItem[_currentId].text[3]);
			
			
			calendar_btn.setLang(_data.lang);
			programm_btn.setLang(_data.lang);
			
			_currentId = _data.params.selectedId;
			
			setContentParam(_currentId);
			
			checkedNavigation();
			
			setEvents();
		};
		
		private function checkedNavigation():void
		{
			// проверяем навигацию
			// проверка
			////trace('checkedNavigation---+++',_currentId);
			if (_currentId>=1) 
			{
				TweenLite.to(narrow_left, TIME_HIDE_NAVIGATE, { alpha:1 } );
				narrow_left.addEventListener(MouseEvent.CLICK, onClickLeftBtn);
				_isLast = true;
				narrow_left.selected = !_isLast;
			}else
			{
				narrow_left.removeEventListener(MouseEvent.CLICK, onClickLeftBtn);
				_isLast = false;
				narrow_left.selected = !_isLast;
				TweenLite.to(narrow_left, TIME_HIDE_NAVIGATE, { alpha:0 } );
			}
			
			if (_currentId<_arrayDataItem.length-1) 
			{
				TweenLite.to(narrow_right, TIME_HIDE_NAVIGATE, { alpha:1 } );
				narrow_right.addEventListener(MouseEvent.CLICK, onClickRightBtn);
				_isNext = true;
				narrow_right.selected = !_isNext;
			}else
			{
				narrow_right.removeEventListener(MouseEvent.CLICK, onClickRightBtn);
				_isNext = false;
				narrow_right.selected = !_isNext;
				TweenLite.to(narrow_right, TIME_HIDE_NAVIGATE, { alpha:0 } );
			}
			
		}
		
		
		/**
		 * установим нужные параметры сонтенту
		 */
		private function setContentParam(n:int):void
		{
			trace('set_ContentParam = ', n);
			
			dates.text = _data.params.data.fullDateString;
			
			setParamHead(HeadMarkerBase.BGD_RED, _arrayDataItem[_currentId].text[2]);
			time.text = _arrayDataItem[_currentId].text[1];
			textEvent.htmlText = _arrayDataItem[_currentId].text[3];
			
			if (_arrayDataItem[_currentId].image[1].image_path!='') 
			{
				_image.loadImage(RemoteProxy.DOMEN + _arrayDataItem[_currentId].image[1].image_path);
			}else
			{
				
			}
			
		}
		
		/**
		 * переход на календарь
		 */
		private function onCLickCalendarBtn(e:MouseEvent):void 
		{
			removeEvents();
			
			var obj:SectionVO = new SectionVO();
			obj.id = SectionType.PAGE_EXPO_EVENTS_CALENDAR;
			obj.currentId = id;
			obj.params = null;
			
			var ev:UIEvent = new UIEvent(UIEvent.SECTION_CHANGE);
			ev.data = obj;
			dispatchEvent(ev);
			
		}
		
		/**
		 * переход на программу на сегодня !!!
		 * @param	e
		 */
		private function onCLickProgrammBtn(e:MouseEvent):void 
		{
			removeEvents();
			
			var obj:SectionVO = new SectionVO();
			obj.id = SectionType.PAGE_EXPO_TODAY_PROGRAMM;
			obj.currentId = id;
			// возможно надо today убрать, для теста можно подставить _data.params.data
			obj.params = {today :  _data.params.today, selected : _data.params.today};
			
			var ev:UIEvent = new UIEvent(UIEvent.SECTION_CHANGE);
			ev.data = obj;
			dispatchEvent(ev);
			
		}
		
		/**
		 * предыдущее событие
		 * @param	e
		 */
		private function onClickLeftBtn(e:MouseEvent):void 
		{
			_currentId = _currentId - 1;
			setContentParam(_currentId);
			checkedNavigation();
		}
		
		/**
		 * следущее события
		 * @param	e
		 */
		private function onClickRightBtn(e:MouseEvent):void 
		{
			_currentId = _currentId + 1;
			setContentParam(_currentId);
			checkedNavigation();
		}
	}

}